/*
The Power of History
by Yiqing Xu and Jiannan Zhao
*/

// install packages
ssc install outreg2
ssc install reghdfe
ssc install ftools

// start
set more off

// delete redundant files
cap erase "tables/sumstat.txt"
cap erase "tables/balance.txt"
cap erase "tables/balance.txt"
cap erase "tables/nat.txt"
cap erase "tables/nat_hetero.txt"
cap erase "tables/system.txt"
cap erase "tables/system_hetero.txt"
cap erase "tables/foreign_policy.txt"
cap erase "tables/foreign_policy_hetero.txt"

use "victimization.dta", clear

** create variable lists
global dv = "nat pat system perception hawk"
global covars = "age age_sq female hschool jcollege college income substatus knwl minority ccp ideology*"
global tr = "tr_victim tr_liberal tr_olympic"


cap erase "tables/sumstat.xml"
** summary stats
set matsize 1000
g emotional = treat == 0 | t1emo == 1 | t2emo == 1 | t3emo == 1
g agree = treat == 0 |t1agree == 1 | t2agree == 1 | t3agree == 1
g right = treat == 0 | t1right == 1 | t2right == 1 | t3right == 1
g min10 = mins >= 10
outreg2 using "tables/sumstat.xml", sum(log) replace eqkeep(N mean sd max) keep(age female hschool jcollege college income substatus knwl minority ccp ideology*)


cap erase "tables/balance.xml"
** balance
qui foreach var of varlist $covars {
	reg `var' $tr, r
	test $tr
	outreg2 using "tables/balance.xml", excel dec(3) keep($tr) addstat(F test, e(p))
}


** victim and patriotic sentiment 
cap erase "tables/nat.xml"
qui foreach var of varlist nat nat? pat pat? {

	reghdfe `var' $tr $covars, a(prov) vce(r)
	outreg2 using "tables/nat.xml", excel dec(3) keep($tr) nocons

	reghdfe `var' $tr $covars if eduy <16, a(prov) vce(r)
	outreg2 using "tables/nat.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if eduy >=16, a(prov) vce(r)
	outreg2 using "tables/nat.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if treat == 0 | t1emo == 1 | t2emo == 1 | t3emo == 1, a(prov) vce(r)
	outreg2 using "tables/nat.xml", excel dec(3) keep($tr) nocons

	reghdfe `var' $tr $covars if treat == 0 | t1nonemo == 1 | t2nonemo == 1 | t3nonemo == 1, a(prov) vce(r)
	outreg2 using "tables/nat.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if treat == 0 |t1agree == 1 | t2agree == 1 | t3agree == 1, a(prov) vce(r)
	outreg2 using "tables/nat.xml", excel dec(3) keep($tr) nocons

	reghdfe `var' $tr $covars if treat == 0 | t12 < 4 | t22 < 4 | t32 < 4, a(prov) vce(r)
	outreg2 using "tables/nat.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if treat == 0 | t1right == 1 | t2right == 1 | t3right == 1, a(prov) vce(r)
	outreg2 using "tables/nat.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if mins >= 10, a(prov) vce(r)
	outreg2 using "tables/nat.xml", excel dec(3) keep($tr) nocons
}



** other heterogenous effect
* victim and pat
cap erase "tables/nat_hetero.xml"
qui foreach var of varlist nat pat {
	reghdfe `var' $tr $covars if lincome == 1, a(prov) vce(r)
	outreg2 using "tables/nat_hetero.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if hincome == 1, a(prov) vce(r)
	outreg2 using "tables/nat_hetero.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if knwl <=1, a(prov) vce(r)
	outreg2 using "tables/nat_hetero.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if knwl >=4, a(prov) vce(r)
	outreg2 using "tables/nat_hetero.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if ccp == 0, a(prov) vce(r)
	outreg2 using "tables/nat_hetero.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if ccp == 1, a(prov) vce(r)
	outreg2 using "tables/nat_hetero.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if eng <= 2, a(prov) vce(r)
	outreg2 using "tables/nat_hetero.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if eng >= 4, a(prov) vce(r)
	outreg2 using "tables/nat_hetero.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if vpn == 0, a(prov) vce(r)
	outreg2 using "tables/nat_hetero.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if vpn == 1, a(prov) vce(r)
	outreg2 using "tables/nat_hetero.xml", excel dec(3) keep($tr) nocons
}



** Support for the political system
cap erase "tables/system.xml"
qui foreach var of varlist system system? {

	reghdfe `var' $tr $covars, a(prov) vce(r)
	outreg2 using "tables/system.xml", excel dec(3) keep($tr) nocons

	reghdfe `var' $tr $covars if eduy <16, a(prov) vce(r)
	outreg2 using "tables/system.xml", excel dec(3) keep($tr) nocons

	reghdfe `var' $tr $covars if eduy >=16, a(prov) vce(r)
	outreg2 using "tables/system.xml", excel dec(3) keep($tr) nocons
		
	reghdfe `var' $tr $covars if treat == 0 |t1emo == 1 | t2emo == 1 | t3emo == 1, a(prov) vce(r)
	outreg2 using "tables/system.xml", excel dec(3) keep($tr) nocons

	reghdfe `var' $tr $covars if treat == 0 | t1nonemo == 1 | t2nonemo == 1 | t3nonemo == 1, a(prov) vce(r)
	outreg2 using "tables/system.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if treat == 0 |t1agree == 1 | t2agree == 1 | t3agree == 1, a(prov) vce(r)
	outreg2 using "tables/system.xml", excel dec(3) keep($tr) nocons

	reghdfe `var' $tr $covars if treat == 0 | t12 < 4 | t22 < 4 | t32 < 4, a(prov) vce(r)
	outreg2 using "tables/system.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if treat == 0 | t1right == 1 | t2right == 1 | t3right == 1, a(prov) vce(r)
	outreg2 using "tables/system.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if mins >= 10, a(prov) vce(r)
	outreg2 using "tables/system.xml", excel dec(3) keep($tr) nocons
}

** other heterogenous effect
* system support
cap erase "tables/system_hetero.xml"
qui foreach var of varlist system {
	reghdfe `var' $tr $covars if lincome == 1, a(prov) vce(r)
	outreg2 using "tables/system_hetero.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if hincome == 1, a(prov) vce(r)
	outreg2 using "tables/system_hetero.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if knwl <=1, a(prov) vce(r)
	outreg2 using "tables/system_hetero.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if knwl >=4, a(prov) vce(r)
	outreg2 using "tables/system_hetero.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if ccp == 0, a(prov) vce(r)
	outreg2 using "tables/system_hetero.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if ccp == 1, a(prov) vce(r)
	outreg2 using "tables/system_hetero.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if eng <= 2, a(prov) vce(r)
	outreg2 using "tables/system_hetero.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if eng >= 4, a(prov) vce(r)
	outreg2 using "tables/system_hetero.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if vpn == 0, a(prov) vce(r)
	outreg2 using "tables/system_hetero.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if vpn == 1, a(prov) vce(r)
	outreg2 using "tables/system_hetero.xml", excel dec(3) keep($tr) nocons
}



** Foreign Policy
cap erase "tables/foreign_policy.xml"
qui foreach var of varlist perception perception? hawk hawk? {

	reghdfe `var' $tr $covars, a(prov) vce(r)
	outreg2 using "tables/foreign_policy.xml", excel dec(3) keep($tr) nocons

	reghdfe `var' $tr $covars if eduy <16, a(prov) vce(r)
	outreg2 using "tables/foreign_policy.xml", excel dec(3) keep($tr) nocons

	reghdfe `var' $tr $covars if eduy >=16, a(prov) vce(r)
	outreg2 using "tables/foreign_policy.xml", excel dec(3) keep($tr) nocons 
	
	reghdfe `var' $tr $covars if treat == 0 |t1emo == 1 | t2emo == 1 | t3emo == 1, a(prov) vce(r)
	outreg2 using "tables/foreign_policy.xml", excel dec(3) keep($tr) nocons

	reghdfe `var' $tr $covars if treat == 0 | t1nonemo == 1 | t2nonemo == 1 | t3nonemo == 1, a(prov) vce(r)
	outreg2 using "tables/foreign_policy.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if treat == 0 |t1agree == 1 | t2agree == 1 | t3agree == 1, a(prov) vce(r)
	outreg2 using "tables/foreign_policy.xml", excel dec(3) keep($tr) nocons

	reghdfe `var' $tr $covars if treat == 0 | t12 < 4 | t22 < 4 | t32 < 4, a(prov) vce(r)
	outreg2 using "tables/foreign_policy.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if treat == 0 | t1right == 1 | t2right == 1 | t3right == 1, a(prov) vce(r)
	outreg2 using "tables/foreign_policy.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if mins >= 10, a(prov) vce(r)
	outreg2 using "tables/foreign_policy.xml", excel dec(3) keep($tr) nocons
}



** other heterogenous effect
** foreign policy
cap erase "tables/foreign_policy_hetero.xml"
qui foreach var of varlist perception hawk {
	reghdfe `var' $tr $covars if lincome == 1, a(prov) vce(r)
	outreg2 using "tables/foreign_policy_hetero.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if hincome == 1, a(prov) vce(r)
	outreg2 using "tables/foreign_policy_hetero.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if knwl <=1, a(prov) vce(r)
	outreg2 using "tables/foreign_policy_hetero.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if knwl >=4, a(prov) vce(r)
	outreg2 using "tables/foreign_policy_hetero.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if ccp == 0, a(prov) vce(r)
	outreg2 using "tables/foreign_policy_hetero.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if ccp == 1, a(prov) vce(r)
	outreg2 using "tables/foreign_policy_hetero.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if eng <= 2, a(prov) vce(r)
	outreg2 using "tables/foreign_policy_hetero.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if eng >= 4, a(prov) vce(r)
	outreg2 using "tables/foreign_policy_hetero.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if vpn == 0, a(prov) vce(r)
	outreg2 using "tables/foreign_policy_hetero.xml", excel dec(3) keep($tr) nocons
	
	reghdfe `var' $tr $covars if vpn == 1, a(prov) vce(r)
	outreg2 using "tables/foreign_policy_hetero.xml", excel dec(3) keep($tr) nocons
}


// delete redundant files
cap erase "tables/sumstat.txt"
cap erase "tables/balance.txt"
cap erase "tables/balance.txt"
cap erase "tables/nat.txt"
cap erase "tables/nat_hetero.txt"
cap erase "tables/system.txt"
cap erase "tables/system_hetero.txt"
cap erase "tables/foreign_policy.txt"
cap erase "tables/foreign_policy_hetero.txt"
